from django.contrib.postgres.fields import JSONField, ArrayField
from django.db import models
from django.utils import timezone


# Create your models here.
class WearTag(models.Model):
    institute = models.IntegerField(null=True, blank=True, verbose_name="顶级组织id", db_index=True)
    org = models.IntegerField(null=True, blank=True, verbose_name="组织")
    farm = models.IntegerField(null=True, blank=True, verbose_name="农场")
    scene = models.CharField(max_length=128, null=True, blank=True, verbose_name="场景")
    eartag = models.CharField(max_length=64, null=True, blank=True, verbose_name="耳标号")
    epc = models.CharField(max_length=64, null=True, blank=True, verbose_name="epc")
    tid = models.CharField(max_length=64, null=True, blank=True, verbose_name="tid")
    operate_time = models.DateTimeField(null=True, blank=True, verbose_name="戴标时间")
    longitude = models.CharField(max_length=32, null=True, blank=True, verbose_name="经度")
    latitude = models.CharField(max_length=32, null=True, blank=True, verbose_name="纬度")
    weartag_addr = models.CharField(max_length=64, null=True, blank=True, verbose_name="戴标地点")
    device_name = models.CharField(max_length=32, null=True, blank=True, verbose_name="设备名称")
    device_type = models.CharField(max_length=32, null=True, blank=True, verbose_name="设备型号")
    mac_addr = models.CharField(max_length=32, null=True, blank=True, verbose_name="MAC地址")
    app_version = models.CharField(max_length=32, null=True, blank=True, verbose_name="APP版本号")
    template = models.IntegerField(null=True, blank=True, verbose_name="模板")
    custom_field = JSONField(null=True, blank=True)
    weartag_type = models.IntegerField(default=2, verbose_name="记录打标形式")  # 0: 自动识别  1: 打标后识别  2: 其他情况
    eartag_type = models.IntegerField(null=True, blank=True,
                                      verbose_name="耳标状态")  # null:正常戴标耳标，0:未领用，1:已领用，2:使用中，3:已死亡，4:已淘汰，5:已核销，6:已归还，7:已报损, 8:非在库耳标，9:非所属农场耳标，10:非规则耳标
    account_set_id = models.IntegerField(null=True, blank=True, verbose_name="账套id")
    flag = models.CharField(max_length=32, null=True, blank=True, verbose_name="标志位-区分配置功能", db_index=True)
    # http://api.dev.smartahc.com/aiot/appm-service/custom/menu/?institute=3&page=1&page_size=20&search_category=1这个flag是这个接口的id
    create_time = models.DateTimeField(default=timezone.now, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    is_active = models.BooleanField(default=True, verbose_name="假删除")  # False表示删除

    class Meta:
        db_table = "weartag"
        verbose_name = "戴标表"
        verbose_name_plural = "戴标表"

    def __str__(self):
        return self.eartag
